package com.howells.time.base.http;

import com.alibaba.fastjson.JSON;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;

/**
 * Created by Administrator on 2016.3.27.
 */
public class HttpUtils {

    public static void Get(String url, Callback callback) {

        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(callback);
    }
    public static void Get(String url,Map heads,Callback callback){
        if(heads==null){
            Get(url,callback);
        }else {
            OkHttpUtils
                    .get()
                    .url(url)
                    .headers(heads)
                    .build()
                    .execute(callback);
        }
    }

    public static void PostObject(String url, Object object, Callback callback) {
        PostObject(url,object,"application/json",callback);
    }

    public static void PostObject(String url, Object object,String type, Callback callback) {
        OkHttpUtils
                .postString()
                .url(url)
                .mediaType(MediaType.parse(type))
                .content(JSON.toJSONString(object))
                .build()
                .execute(callback);
    }

    public static void PostObject(String url,Object object,Map heads,String type,Callback callback){
        if(heads==null){
            PostObject(url,object,type,callback);
        }else{
            OkHttpUtils
                    .postString()
                    .url(url)
                    .mediaType(MediaType.parse(type))
                    .headers(heads)
                    .content(JSON.toJSONString(object))
                    .build()
                    .execute(callback);
        }
    }

    public static void PostFile(String url, File file, Callback callback) {
        if (!file.exists()) {
            return;
        }
        OkHttpUtils
                .postFile()
                .url(url)
                .file(file)
                .build()
                .execute(callback);
    }

    public static void PostFile(String url,File file,Map head,Callback callback){
        if (!file.exists()) {
            return;
        }
        OkHttpUtils
                .postFile()
                .url(url)
                .headers(head)
                .file(file)
                .build()
                .execute(callback);
    }

    public static void UploadFile(String url,File file,Map headers,Map params,Callback callback){
        if(!file.exists()){
            return;
        }
        if(headers!=null) {
            OkHttpUtils.post()//
                    .addFile("mFile", file.getName(), file)//
                    .url(url)//
                    .params(params)//
                    .headers(headers)//
                    .build()//
                    .execute(callback);
        }else {
            UploadFile(url,file,params,callback);
        }
    }
    public static void UploadFile(String url, File file, Map<String, Object> params, Callback callback) {
        if (!file.exists()) {
            return;
        }

        Map<String,String> param= new HashMap<>();
        for(String str:params.keySet()){
            param.put(str,params.get(str).toString());
        }

        OkHttpUtils.post()//
                .addFile("mFile",file.getName(), file)//
                .url(url)//
                .params(param)//
                .build()//
                .execute(callback);
    }

    public static void getImage(String url,Callback callback){
        OkHttpUtils
                .get()//
                .url(url)//
                .build()//
                .connTimeOut(20000)
                .readTimeOut(20000)
                .writeTimeOut(20000)
                .execute(callback);
    }
    public static void DownloadFile(String url,Callback callback){
        OkHttpUtils//
                .get()//
                .url(url)//
                .build()//
                .execute(callback);
    }
}
